package DBLayer;

import ControlLayer.GlobalInstances;
import DBLayer.Interfaces.IContainer;
import ModelLayer.Order;
import ModelLayer.OrderLine;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DBOrder extends DatabaseAccess implements IContainer<Order>
{
    DBOrderLine DBorderline = new DBOrderLine();
    DBProduct DbProduct = new DBProduct();

    public DBOrder()
    {
        super();
    }

    public Order getObject(int ID)
    {
        String query = "Select OrderID as ID, [Date], SupplierID, EmployeeID, DeliveryStaus, DeliveryDate From [Order] Where OrderID = " + ID;
        ResultSet result = ExecuteQuery(query);

        GenericModelBinder<Order> modelBinder = new GenericModelBinder<Order>(Order.class);

        try
        {
            return modelBinder.BindModel(result);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public ArrayList<Order> getObjects()
    {
        String query = "Select OrderID as ID, [Date], SupplierID, EmployeeID, DeliveryStaus, DeliveryDate From [Order]";
        ResultSet result = ExecuteQuery(query);

        GenericModelBinder<Order> modelBinder = new GenericModelBinder<Order>(Order.class);

        try
        {
            return modelBinder.BindModels(result);
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (InstantiationException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IllegalAccessException ex)
        {
            Logger.getLogger(DBEmployee.class.getName()).log(Level.SEVERE, null, ex);
        }

        return null;
    }

    public Order attachModels(Order order)
    {
        int orderID = order.getOrderID();
        order.setOrderLines(DBorderline.getOrderLinesByOrderID(orderID));
        return order;
    }

    public boolean SaveObject(Order object)
    {
        if (object == null)
        {
            return false;
        }
        java.util.Date today = new java.util.Date();
        java.sql.Date newDate = new java.sql.Date(today.getTime());

        String newSql = "INSERT INTO [Order] VALUES (?,?,?,?,?)";

        try
        {
            PreparedStatement statement = dbConnection.prepareStatement(newSql, PreparedStatement.RETURN_GENERATED_KEYS);
            statement.clearParameters();
            
            statement.setDate(1, newDate);
            statement.setInt(2, object.getSupplier().getSupplierID());
            statement.setInt(3, object.getEmployee().getEmployeeID());
            statement.setString(4, object.getDeliveryStatus());
            statement.setString(5, object.getDeliveryDate());

            statement.executeUpdate();
            ResultSet generatedKeys = statement.getGeneratedKeys();
            int generatedID = 0;
            if (generatedKeys.next())
            {
                generatedID = generatedKeys.getInt(1);

                object.setOrderID(generatedID);

                for (OrderLine orderLine : object.getOrderLines())
                {
                    orderLine.getProduct().setSupplier(object.getSupplier());
                    DbProduct.SaveObject(orderLine.getProduct());
                    
                    orderLine.setOrder(object);
                    if (!DBorderline.SaveObject(orderLine))
                    {
                        System.out.println("Error saving orderline. Orderid: " + orderLine.getOrder().getOrderID() + " productID: " + orderLine.getProduct().getProductID());
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(DBOrder.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public boolean DeleteObject(Order object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean UpdateObject(Order object)
    {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
